package main.java.day06;

import com.sun.org.apache.bcel.internal.generic.FSUB;

import java.io.*;
import java.util.Scanner;

/**
 * 要求用户输入一个文件名，并复制当前目录中该文件，并取名为"原文件名_copy.后缀名"
 * 定义两个方法分别使用单字节形式复制，以及字节数组形式复制
 * @author Xiloer
 *
 */
public class Test07 {
	public static void main(String[] args) throws IOException {
		System.out.println("请输入一个要复制的文件名:");
		Scanner scanner = new Scanner(System.in);
		String from = scanner.nextLine();
		Test07 t = new Test07();
		t.copy1(from);
		t.copy2(from);

	}

	/**
	 * 使用单字节方式复制
	 *
	 * @param name 要复制的文件名
	 */
	public void copy1(String name) throws IOException {
		FileInputStream in = new FileInputStream(name);
		String to = name.split(".")[0] + "_copy." + name.split(".")[1];
		FileOutputStream out = new FileOutputStream(to);
		int b = 0;
		while ((b = in.read()) != -1) {
			out.write(b);
		}
		in.close();
		out.close();
	}

	/**
	 * 使用字节数组形式复制
	 *
	 * @param name 要复制的文件名
	 */
	public void copy2(String name) throws IOException {
		FileInputStream in = new FileInputStream(name);
		String to = name.split(".")[0] + "_copy." + name.split(".")[1];
		FileOutputStream out = new FileOutputStream(to);
		int b = 0;
		byte[] buff = new byte[8192];
		while ((b = in.read(buff)) != -1) {
			out.write(b);
			in.close();
			out.close();
		}
	}
}
